﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.Security;

namespace Co9320.NamaAlert.Extension.Model
{
    class Setting : ISetting
    {
        private IPluginSettings setting;

        public Setting(IPluginSettings setting)
        {
            this.setting = setting;

            userName = setting.GetString("UserName");
            password = new SecureString();
            foreach (char c in setting.GetString("Password", "").ToCharArray())
                password.AppendChar(c);
        }

        public IPluginSettings PluginSettings
        {
            get { return setting; }
        }

        #region ISetting メンバ

        private string userName;
        public string UserName
        {
            get { return userName; }
            set
            {
                userName = value;
                setting.SetString("UserName", userName);
                OnChangedSetting(EventArgs.Empty);
            }
        }

        private SecureString password;
        public SecureString Password
        {
            get { return password; }
            set
            {
                password = value;
                setting.SetString("Password", Marshal.PtrToStringBSTR(Marshal.SecureStringToBSTR(password)));
                OnChangedSetting(EventArgs.Empty);
            }
        }

        public event EventHandler ChangedSetting;

        #endregion

        protected void OnChangedSetting(EventArgs e)
        {
            if (ChangedSetting != null)
                ChangedSetting(this, e);
        }
    }
}
